package com.rao.gmall.product.controller;

import com.rao.gmall.common.result.Result;
import com.rao.gmall.model.product.BaseTrademark;
import com.rao.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.codec.language.Nysiis;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author nzmxfan
 * @create 2021-08-03-17:03
 */
@RestController
@RequestMapping("/admin/product")
public class BaseTrademarkController {

    @Resource
    BaseTrademarkService baseTrademarkService;

    /**
     *  查询品牌列表
     * @param page 当前页码
     * @param limit 每页数据的数量
     * @return
     */
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result<Page<BaseTrademark>> getBaseTrademark(@PathVariable("page") Long page,
                                                        @PathVariable("limit") Long limit){

        Page<BaseTrademark> baseTrademarkPage = new Page<>(page, limit);

        Page<BaseTrademark> pageData = baseTrademarkService.page(baseTrademarkPage);

        return Result.ok(pageData);

    }

    /**
     *  新增品牌
     * @param baseTrademark 品牌内容
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark){

        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }

    /**
     *  修改品牌属性
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result update(@RequestBody BaseTrademark baseTrademark){
        boolean update = baseTrademarkService.updateById(baseTrademark);
        if (update){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     *  根据品牌 id 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        baseTrademarkService.delete(id);
//        baseTrademarkService.removeById(id);
        return Result.ok();
    }

    /**
     *  根据品牌 id 获取品牌信息
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result<BaseTrademark> get(@PathVariable("id") Long id){
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        if (baseTrademark != null){
            return Result.ok(baseTrademark);
        }else {
            return Result.fail();
        }
    }

    @GetMapping("/baseTrademark/getTrademarkList")
    public Result<List<BaseTrademark>> getTrademarkList(){

        List<BaseTrademark> list = baseTrademarkService.list();
        return Result.ok(list);
    }
}
